<?php
/**
 * Created by PhpStorm.
 * User: 曹乐清
 * Date: 2017/3/21
 * Time: 15:00
 */

namespace Home\Controller;


class StaffController extends CommonController
{
    public function index(){
        $roue=D('auth_group')->select();
        $this->assign('roue',$roue);
        $this->display();
    }

    //@todo 获取用户列表
    public function get_user_list(){
        $count=D('user')->where(array('status'=>0))->count();
        $list=D('user')->field("*,FROM_UNIXTIME(ac_time) as ac_time")
            ->join("left join wx_auth_group_access as au on au.uid=wx_user.id")
            ->where(array('status'=>0))->limit($_GET['offset'],$_GET['limit'])->order('id desc')->select();
    foreach($list as $key=>$value){
        if($value['group_id']){
            $group=D('auth_group')->find($value['group_id']);
            $list[$key]['group_title']=$group['title'];
        }

    }
        echo json_encode(array('rows'=>$list,'total'=>$count));
    }

    //@todo 新增用户
    public function add_user(){
        $name=$_POST['name'];
        $password=$_POST['password'];
        $add=D('user')->add(array('name'=>$name,'password'=>md5($password),'ac_time'=>time()));
        if(!$add){
            $this->ajaxReturn(array('success'=>false,'message'=>'新增失败请稍后再试'));
        }
        $ad=D('auth_group_access')->add(array('uid'=>$add,'group_id'=>$_POST['group_id']));
        if(!$ad){
            $this->ajaxReturn(array('success'=>false,'message'=>'新增失败请稍后再试'));
        }
        $this->ajaxReturn(array('success'=>true,'message'=>'新增成功'));
    }
    //@todo 修改密码
    public function edit_pass(){
        $id=$_POST['id'];
        $password=$_POST['pass'];
        $data=array(
            'id'=>$id,
            'password'=>md5($password)
        );
        $res=D('user')->save($data);
        if(!is_numeric($res)){
            $this->ajaxReturn(array('success'=>false,'message'=>'密码修改失败,请稍后再试'));
        }
        $this->ajaxReturn(array('success'=>true,'message'=>'密码修改成功,请重新登录'));

    }

    //@todo 删除用户
    public function del_user(){
        $del=D('user')->delete($_GET['id']);
        if(!$del){
            $this->ajaxReturn(array('success'=>false,'message'=>'删除失败,请稍后再试'));
        }
        $de=D('auth_group_access')->where(array('uid'=>$_GET['id']));
        if(!$de){
            $this->ajaxReturn(array('success'=>false,'message'=>'删除失败,请稍后再试'));
        }
        $this->ajaxReturn(array('success'=>true,'message'=>'删除成功'));
    }
}